# -*- coding: utf-8 -*-
import re
from mongo import *
from setting import *
__author__ = 'PP'
db = MongoDb()
def convert(s,k,v):
    print '%s,%s'%(s,k)
    attr = 'attrs.'+k
    search = v['search']
    value = v['value']
    unit = ''
    isMuti = False
    isFixed = False
    isSelect = False
    if v.has_key('unit'):
        unit = v['unit']
    if v.has_key('isMuti'):
        isMuti = v['isMuti']
    if v.has_key('isFixed'):
        isFixed = v['isFixed']
    if v.has_key('isSelect'):
        isSelect = v['isSelect']
    if v.has_key('default'):
        default = v['default']
    else:
        default = ''
    #list = db.find({'sort':s,'attrs.网络类型':None}) #'$regex':search,'$options':'i',
    list = db.find({'sort':s,attr: {'$exists': True,'$regex':search,'$options':'i','$type':2}})
    #list = db.find({'sort':s,attr: {'$exists': True,'$type':3}})
    for item in list:
        source = item['attrs'][k.decode('utf-8')]
        #source = item['attrs'][k.decode('utf-8')]['t']
        newValue = None
        if isFixed:     #固定转换
            if value.has_key(source.encode('utf-8')):
                cv = value[source.encode('utf-8')]
                newValue = {'v':cv,'t':source}
            else:
                print '%s not exists'%source
        elif isSelect:
            cv=[]
            if len(source.strip())==0:
                cv.append(default)
            else:
                v =  re.findall(value.decode('utf-8'),source, re.I)
                for na in v:
                    for n in na:
                        if len(n.strip())>0:
                            cv.append(n.strip());
            newValue = {'v':cv,'t':source}
            #print source
            #print cv
            #print value
        else:
            v =  re.findall(value.decode('utf-8'),source)
            u = re.findall(unit.decode('utf-8'),source)
            if len(v)==0:
                continue
            if len(u)==0:
                u = default
            if isMuti: #目前用于处理分辨率数据
                newValue = {'v':{'h':float(v[0][0]),'w':float(v[0][1])},'u':u[0],'t':source}
            else:
                cu = u[0]
                cv = float(v[0])
                if unitsetting[s].has_key(k):   #需要单位转换
                    cunits = unitsetting[s][k]
                    scale = 1
                    if cunits.has_key(cu):
                        scale = cunits[cu]
                    newValue = {'v':cv*scale,'u':cu,'t':source}
                    #print '%sx%s=%s'%(cv,scale,cv*scale)
                else:
                    newValue = {'v':cv,'u':cu,'t':source}
        if newValue:
            db.save(item['_id'],{'attrs.'+k:newValue})
            print 'id:%s = %s'%(item["_id"],newValue)

#
# list = db.find({'sort':'手机','attrs.电池容量': {'$exists': True,'$regex':'(\d+).+','$options':'i','$type':2}})
# for item in list:
#     #db.save(item)
#     value = item['attrs']['电池容量'.decode('utf-8')]
#     v =  re.findall(r"(\d+).+",value)[0]
#     u = re.findall(r"\d+(.+)",value)[0]
#     newValue = {'v':int(v),'u':u,'t':value}
#     #db.save(item['_id'],{'attrs.电池容量':newValue})
#     print 'id:%s = %d %s'%(item["_id"],int(v),u)
#     #print value
# print 'count:%d'%list.count()
